﻿@{
    Bundle("jquery_validate");
}

<table>
    <tr>
        <td style="vertical-align: top; width: 40%;">
            <script type="text/javascript">
                jQuery(document).ready(function () {
                    jQuery('@ID("#form")').validate({
                        rules: {
                            tagName: {
                                required: true
                            }
                        }
                    });
                    jQuery('@ID("#txt_tag")').autocomplete({
                        source: function (request, response) {
                            jQuery.post('@Html.Raw(Url.Action(Loc.Act("Autocomplete").Ctrl("Tag").Area("Blog").Localized()))', request, response);
                        },
                        minLength: 3,
                        select: function (event, ui) {
                            jQuery('@ID("#hidden_tag")').val(ui.item.id);
                        }
                    });
                });
            </script>
            <form class="common-form" id="@ID("form")" action="@Url.Action(Loc.Act("MapTag").Localized())" method="post">
                <input type="hidden" name="id" value="@JDM.Int32(Model.EntityID)" />
                <ul>
                    <li>
                        <input id="@ID("hidden_tag")" type="hidden" name="tagID" />
                        <input id="@ID("txt_tag")" type="text" name="tagName" />
                    </li>
                </ul>
                <ul class="bar toolbar">
                    <li><button class="button" type="submit">@T(Res.In("Common").Of("Act").Name("Submit"))</button></li>
                </ul>
            </form>
        </td>
        <td style="vertical-align: top; width: 60%; padding: 0 1em;">
            <ul class="cloud bordered">
            @foreach(var i in Model.Tags)
            {
                var t = (i.Translations as IEnumerable<dynamic>)
                    .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                        JDM.Int32(Svc.Localization.Language.EntityID));
                <li>
                    <ul class="bar inlinebar">
                        <li><a class="confirm" href="@Url.Action(Loc.Act("UnmapTag").Set(new
                        {
                            id = JDM.Int32(Model.EntityID), 
                            tagID = JDM.Int32(i.EntityID)
                        }).Localized())">x</a></li>
                    </ul>
                    @String.Format("{0} ({1})", t != null ? JDM.String(t.Name) : JDM.Int32(i.EntityID), JDM.Int32(i.Weight))
                </li>
            }
            </ul>
        </td>
    </tr>
</table>